//
//  MineAddBankCountViewController.m
//  renmingongshe
//
//  Created by macbook on 2017/11/9.
//  Copyright © 2017年 xiaodian. All rights reserved.
//

#import "MineAddBankCountViewController.h"
#import "ChooseBankViewController.h"

@interface MineAddBankCountViewController ()<UIPickerViewDelegate, UIPickerViewDataSource>
{
    NSMutableArray *_bankArr;
    NSMutableArray *_cityArr;
    NSString *_type;
    NSString *_bankId;
}
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottom;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UITextField *NameTF;
@property (weak, nonatomic) IBOutlet UITextField *PhoneTF;
@property (weak, nonatomic) IBOutlet UITextField *UserIdTF;
@property (weak, nonatomic) IBOutlet UILabel *BankName;
@property (weak, nonatomic) IBOutlet UILabel *ShengLabel;
@property (weak, nonatomic) IBOutlet UILabel *CityLabel;
@property (weak, nonatomic) IBOutlet UILabel *XianLabel;
@property (weak, nonatomic) IBOutlet UITextField *OpenBankTF;
@property (weak, nonatomic) IBOutlet UITextField *CarderTF;
@property (weak, nonatomic) IBOutlet UITextField *CardNumTF;

@property (strong, nonatomic) UIPickerView *pickerView;
@property (strong, nonatomic) UIView *BtnbackView;
@property (strong, nonatomic) UIButton *canceBtn;
@property (copy, nonatomic) NSString *SId;
@property (copy, nonatomic) NSString *CityId;
@property (copy, nonatomic) NSString *AreaId;
@end

@implementation MineAddBankCountViewController

//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    [_NameTF resignFirstResponder];
//    [_PhoneTF resignFirstResponder];
//    [_UserIdTF resignFirstResponder];
//    [_OpenBankTF resignFirstResponder];
//    [_CarderTF resignFirstResponder];
//    [_CardNumTF resignFirstResponder];
//}

- (void)registNotification
{
    // 监听键盘弹出
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyBoard:) name:UIKeyboardWillShowNotification object:nil];
    
    // 监听键盘收起
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hideKeyBoard:) name:UIKeyboardWillHideNotification object:nil];
}

//键盘弹出事件
- (void)showKeyBoard:(NSNotification *)note
{
        float duration = [[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
        CGRect keyboardFrame = [[note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        
        CGFloat keyboardHeight = keyboardFrame.size.height;
        [UIView animateWithDuration:duration animations:^{
            _bottom.constant = keyboardHeight;
        }];
}

// 键盘收起事件
- (void)hideKeyBoard:(NSNotification *)note
{
    float duration = [[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    [UIView animateWithDuration:duration animations:^{
        _bottom.constant=0;
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.titleLabel.text = @"添加账户";
    self.view.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    _cityArr = [NSMutableArray array];
    [self createPickerView];
    [self downloadData];
    [self registNotification];
}

- (void)downloadData
{
    [_cityArr removeAllObjects];
    [self.view loadingOnAnyView];
    [THWManager PostHttp:API(MineChooseBank) Parameters:@{@"userToken":[Utils getUserToken]} Success:^(id responseObject) {
        [self.view removeAnyView];
        _bankArr = [[NSMutableArray alloc] initWithArray:responseObject[@"data"][@"bank"]];
        for (NSDictionary *dic in responseObject[@"data"][@"province"]) {
            [_cityArr addObject:dic];
        }
        [_pickerView reloadAllComponents];
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

//下载城市数据
- (void)downloadCity
{
    [_cityArr removeAllObjects];
    [self.view loadingOnAnyView];
    [THWManager PostHttp:API(AddressChoose) Parameters:@{@"userToken":[Utils getUserToken], @"areaId":_SId} Success:^(id responseObject) {
        [self.view removeAnyView];
        if ([responseObject[@"data"] count] != 0) {
            _cityArr = [[NSMutableArray alloc] initWithArray:responseObject[@"data"]];
        }
        [_pickerView reloadAllComponents];
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

//下载地区数据
- (void)downloadArea
{
    [_cityArr removeAllObjects];
    [self.view loadingOnAnyView];
    [THWManager PostHttp:API(AddressChoose) Parameters:@{@"userToken":[Utils getUserToken], @"areaId":_CityId} Success:^(id responseObject) {
        [self.view removeAnyView];
        if ([responseObject[@"data"] count] != 0) {
            _cityArr = [[NSMutableArray alloc] initWithArray:responseObject[@"data"]];
        }
        [_pickerView reloadAllComponents];
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (void)createPickerView
{
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, HEIGHT-200, WIDTH, 200)];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    _pickerView.backgroundColor = [UIColor colorWithHex:0x999999];
    [_pickerView selectRow:0 inComponent:0 animated:YES];
    _pickerView.hidden = YES;
    [self.view addSubview:_pickerView];
    
    _BtnbackView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT-230, WIDTH, 30)];
    _BtnbackView.backgroundColor = [UIColor colorWithHex:0x999999];
    _BtnbackView.hidden = YES;
    [self.view addSubview:_BtnbackView];
    
    _canceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _canceBtn.frame = CGRectMake(10, 10, 50, 30);
    [_canceBtn setTitle:@"确定" forState:UIControlStateNormal];
    [_canceBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_canceBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [_BtnbackView addSubview:_canceBtn];
}

#pragma mark - UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _cityArr.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *str = _cityArr[row][@"areaName"];
    return str;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if ([_type isEqualToString:@"s"]) {
        _ShengLabel.text = [NSString stringWithFormat:@"%@", _cityArr[row][@"areaName"]];
        _SId = [NSString stringWithFormat:@"%@", _cityArr[row][@"areaId"]];
        _CityLabel.text = @"";
    }else if ([_type isEqualToString:@"city"]) {
        _CityLabel.text = [NSString stringWithFormat:@"%@", _cityArr[row][@"areaName"]];
        _CityId = [NSString stringWithFormat:@"%@", _cityArr[row][@"areaId"]];
        _XianLabel.text = @"";
    }else{
        _XianLabel.text = [NSString stringWithFormat:@"%@", _cityArr[row][@"areaName"]];
        _AreaId = [NSString stringWithFormat:@"%@", _cityArr[row][@"areaId"]];
    }
    
}

- (void)btnClick
{
    _BtnbackView.hidden = YES;
    _pickerView.hidden = YES;
}

- (IBAction)ChooseBank:(UIButton *)sender {
    [_NameTF resignFirstResponder];
    [_PhoneTF resignFirstResponder];
    [_UserIdTF resignFirstResponder];
    [_OpenBankTF resignFirstResponder];
    [_CarderTF resignFirstResponder];
    [_CardNumTF resignFirstResponder];
    ChooseBankViewController *choose = [[ChooseBankViewController alloc] init];
    choose.bankArr = _bankArr;
    choose.backBank = ^(NSString *bankName, NSString *bankId) {
        _BankName.text = [NSString stringWithFormat:@"%@", bankName];
        _bankId = bankId;
    };
    [self.navigationController pushViewController:choose animated:YES];
}

- (IBAction)chooseSheng:(id)sender {
    [_NameTF resignFirstResponder];
    [_PhoneTF resignFirstResponder];
    [_UserIdTF resignFirstResponder];
    [_OpenBankTF resignFirstResponder];
    [_CarderTF resignFirstResponder];
    [_CardNumTF resignFirstResponder];
    [self downloadData];
    _type = @"s";
    _BtnbackView.hidden = NO;
    _pickerView.hidden = NO;
}

- (IBAction)chooseCity:(id)sender {
    [_NameTF resignFirstResponder];
    [_PhoneTF resignFirstResponder];
    [_UserIdTF resignFirstResponder];
    [_OpenBankTF resignFirstResponder];
    [_CarderTF resignFirstResponder];
    [_CardNumTF resignFirstResponder];
    if (![TXUtilsString isBlankString:_SId]) {
        [self downloadCity];
        _type = @"city";
        _BtnbackView.hidden = NO;
        _pickerView.hidden = NO;
    }else{
        [self.view Message:@"请输入省份"];
    }
}

- (IBAction)chooseArea:(id)sender {
    [_NameTF resignFirstResponder];
    [_PhoneTF resignFirstResponder];
    [_UserIdTF resignFirstResponder];
    [_OpenBankTF resignFirstResponder];
    [_CarderTF resignFirstResponder];
    [_CardNumTF resignFirstResponder];
    if (![TXUtilsString isBlankString:_CityId]) {
        [self downloadArea];
        _type = @"area";
        _BtnbackView.hidden = NO;
        _pickerView.hidden = NO;
    }else{
        [self.view Message:@"请输入城市"];
    }
}

- (IBAction)SubmitClick:(UIButton *)sender {
    
    if (_NameTF.text.length == 0 || _PhoneTF.text.length == 0 || _UserIdTF.text.length == 0 || _CardNumTF.text.length == 0 || !_bankId || !_AreaId) {
        [self.view Message:@"请完善信息"];
        return;
    }
    [self.view loadingOnAnyView];
    [THWManager PostHttp:API(MinePickAddCount) Parameters:@{@"userToken":[Utils getUserToken], @"accType":@"3", @"userName":_NameTF.text, @"phone":_PhoneTF.text, @"userCardNo":_UserIdTF.text, @"accNo":_CardNumTF.text, @"accTargetId":_bankId, @"accAreaId":_AreaId} Success:^(id responseObject) {
        [self.view removeAnyView];
        if ([String(responseObject[@"status_code"]) isEqualToString:@"1000"]) {
            [self.view Message:responseObject[@"msg"]];
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC));
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [self.view Message:responseObject[@"msg"]];
        }
        
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
